Type inference for JavaScript

نویسنده

  • Christopher Lyon Anderson
چکیده

Object-oriented scripting languages like JavaScript are popular, in part because of their dynamic features. These include the runtime modification of objects and classes, through addition of fields or updating of methods. These features make static typing difficult and usually dynamic typing is used. Consequently, errors such as access to non-existent members, are not detected until runtime. We provide a static type system that can cope with dynamic features such as member addition, while providing the usual safety guarantees. Since the structure of objects may change over time, we employ a structural type system to track the changes. We show how type inference can be used to infer the structure of objects and give corresponding structural types. Therefore, the programmer can enjoy the safety offered by static typing, without having to give explicit types in their programs. We develop JS0, a formalisation of JavaScript with features including dynamic addition of fields and updating of methods. We give an operational semantics and static type system for JS0 using structural types. Our types allow objects to evolve in a controlled manner by classifying members as definite or potential. A member is potential until it has been assigned to and then it becomes definite. We prove that our type system is sound. We develop a type inference algorithm for JS0 based on a system of constraints between type variables. We define a translation between constraints and types that allows us to generate a type annotated JS0 program from an untype JS0 program. We prove that the constraints are soundwith respect to the type system and that our translation is deterministic. We define a well-formedness criterion on constraints and conjecture that well-formed constraints are satisfiable. Therefore, combined with the soundness of constraints and the soundness of the type system, we conjecture that programs that generate well-formed constraints will not get stuck.

برای دانلود رایگان متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Type Safety for JavaScript

JavaScript is a client-side scripting language for the web and has become increasingly popular with the introduction of AJAX. JavaScript is a dynamic-typed language and is closer to a functional language than a procedural language. Runtime errors can occur in JavaScript due to invalid type conversions and accesses to undefined members of objects. One solution is the use of type systems to valid...

متن کامل

JSTyper: Type inference for JavaScript

As with all untyped languages, JavaScript programs can contain type-mismatch-style errors. These mistakes do not necessarily cause runtime errors and can lead to subtle faults in deployed web apps. Type checking key portions of JavaScript applications can identify these mistakes early. Previous efforts to augment JavaScript with a type system have led to syntax extensions and compulsory type an...

متن کامل

Type Inference for Scripting Languages

JavaScript is a powerful imperative object based language made popular by its use in web pages. It supports flexible program development by allowing dynamic addition of members to objects. Code is dynamically typed: a runtime access to a non-existing member causes an error. In this paper we first develop a formalism of JavaScript, JS0, and a static type system that will detect such type errors....

متن کامل

Type Inference for Static Compilation of JavaScript (Extended Version)

We present a type system and inference algorithm for a rich subset of JavaScript equipped with objects, structural subtyping, prototype inheritance, and first-class methods. The type system supports abstract and recursive objects, and is expressive enough to accommodate several standard benchmarks with only minor workarounds. The invariants enforced by the types enable an ahead-of-time compiler...

متن کامل

Towards Type Inference for JavaScript

Object-oriented scripting languages like JavaScript and Python are popular partly because of their dynamic features. These include the runtime modification of objects and classes through addition of fields or updating of methods. These features make static typing difficult and so usually dynamic typing is used. Consequently, errors such as access to non-existent members are not detected until r...

متن کامل

Towards JavaScript Verification with the Dijkstra State Monad

Several special-purpose systems have been proposed to analyze programs in JavaScript and other dynamically typed languages. However, none of these prior systems support automated, modular verification for both higher-order and stateful features. This paper proposes a new refinement of the state monad, the Dijkstra state monad, as a way of structuring specifications for higher-order, stateful pr...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2006